package cn.moukou.dictator.controller;

import cn.moukou.dictator.service.relation.RelationService;
import cn.moukou.dictator.vo.RelationVO;
import cn.moukou.dictator.dao.relation.*;
import cn.moukou.dictator.pojo.relation.*;
import cn.moukou.dictator.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dictator/api/relation")
public class RelationController {

    @Autowired
    RelationService relationService;


    @RequestMapping("/get/all")
    Resp<Map<String, Object>> getAllRelation() {
        return Resp.ok(relationService.getAllRelationInMap());
    }

    @RequestMapping("/sys/get/all")
    Resp<Map<String, Object>> sysGetAllRelation(@RequestParam("page") Integer page, @RequestParam("page_size") Integer pageSize) {
        return Resp.ok(relationService.getAllRelationInPage(page, pageSize));
    }

    @GetMapping("/sys/get/one")
    public Resp<Map<String, Object>> sysGetRelationById(@RequestParam("id") Integer id) {
        return Resp.ok(relationService.getOneRelation(id));
    }

    @RequestMapping("/get/{type}")
    Resp<Map<String, Object>> getAllRelation(@PathVariable("type") String type, @RequestParam("page") Integer page, @RequestParam("page_size") Integer pageSize) {
        if (page==null) {
            page = 1;
        }
        if(pageSize == null) {
            pageSize = 10;
        }
        return Resp.ok(relationService.getRelationByType(type, page, pageSize));
    }

    @PostMapping("/sys/add")
    Resp<RelationVO> addRelation(@RequestBody Map<String, Object> params) {
        Long start = new Long((Integer)  params.get("start"));
        Long end = new Long((Integer)  params.get("end"));
        String relationType = (String)  params.get("relationType");
        if(start == null || end == null || relationType == null) {
            return Resp.fail("start | end | relationType不能为空");
        } else {
            RelationVO vo = relationService.addRelation(start, end, relationType, params);
            if(vo!=null) {
                return Resp.ok(vo);
            } else {
                return Resp.fail("插入失败");
            }
        }
    }

    @PostMapping("/sys/delete")
    Resp<String> deleteRelationById(@RequestParam("id") Integer id) {
        relationService.deleteRelation(new Long(id));
        return Resp.ok("删除成功");
    }
}
